package com.aurora.app.controller.weixin;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.aurora.app.constant.PayConfig;
import com.aurora.app.constant.WeixinApi;
import com.aurora.app.entity.User;
import com.aurora.app.entity.WeixinAppToken;
import com.aurora.app.service.PayOrderService;
import com.aurora.app.service.WeixinAppTokenService;
import com.aurora.app.util.weixin.PayUtils;
import com.aurora.base.util.QrCodeUtil;
import com.google.zxing.WriterException;
/**
 * 扫描qrcode
 * @author yongjiang.lin
 *
 */
@Controller
@RequestMapping(value="api/qr")
public class QrScanController {
	
	private final Logger LOG = LoggerFactory.getLogger(this.getClass());
	
	@Resource
	private PayOrderService payOrderService;
	@RequestMapping(value="scan")
	public ModelAndView scan(HttpServletRequest request){
		ModelAndView mv = new ModelAndView("test/scanQrCode");
		return mv;
	}
	
	/**
	 * 图片请求生成二维码
	 * @param request
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping(value = "create")
	public void create(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		String openid = ((User)request.getSession().getAttribute("user")).getOpenid();
		//产品id
		String product_id = request.getParameter("product_id");
		if(StringUtils.isBlank(product_id)){
			throw new Exception("物品id不能为空");
		}
		
		//生成订单
		String code_url = this.payOrderService.create(product_id);

		try {
			//生成二维码
			QrCodeUtil.genGR(code_url, response.getOutputStream());
		} catch (WriterException e) {
			e.printStackTrace();
		}
	}
}
